package com.dibang.app.view;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import com.dibang.app.R;
import com.dibang.app.bean.PeerItem;

import org.xml.sax.XMLReader;

import java.util.HashMap;

/**
 * Created by wxl on 2019/7/25.
 *
 */

public class PeerTitleHtmlHandler implements Html.TagHandler {

    public static final String PEER_TITLE_NUM_TAG = "PEER_TITLE_NUM_TAG";

    public static final String PEER_TITLE_TIME_TAG = "PEER_TITLE_TIME_TAG";

    public static final String PEER_TITLE_REMARK_TAG = "PEER_TITLE_REMARK_TAG";

    public  int mStart ;

    private Context context;

    private boolean isFull;

    private HashMap<String, Integer> mMaps = new HashMap<>();

    public PeerTitleHtmlHandler(Context context,boolean isFull) {
        this.context = context;
        this.isFull = isFull;
    }

    @Override
    public void handleTag(boolean opening, String tag, Editable output, XMLReader xmlReader) {
        if (!tag.equals(PEER_TITLE_NUM_TAG) && !tag.equals(PEER_TITLE_TIME_TAG) && !tag.equals(PEER_TITLE_REMARK_TAG)) {
            return;
        }

        if (opening) {
             mStart = output.length();
            if (tag.equals(PEER_TITLE_NUM_TAG)) {
                mMaps.put(PEER_TITLE_NUM_TAG, mStart);
            } else if(tag.equals(PEER_TITLE_TIME_TAG)){
                mMaps.put(PEER_TITLE_TIME_TAG, mStart);
            } else if(tag.equals(PEER_TITLE_REMARK_TAG)){
                mMaps.put(PEER_TITLE_REMARK_TAG, mStart);
            }
        } else {
            int mEnd = output.length();
            if (tag.equals(PEER_TITLE_NUM_TAG)) {
                output.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, isFull?android.R.color.holo_red_dark: R.color.gray_text_color)),
                        mMaps.get(PEER_TITLE_NUM_TAG), mEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else if(tag.equals(PEER_TITLE_TIME_TAG)) {
                output.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.gray_text_color)),
                        mMaps.get(PEER_TITLE_TIME_TAG), mEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            } else if(tag.equals(PEER_TITLE_REMARK_TAG)) {
                output.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.gray_text_color)),
                        mMaps.get(PEER_TITLE_REMARK_TAG), mEnd, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }
    }



    public static void onBindPeerTitleHtmlContent(PeerItem peerItem, TextView titleView){
        String content = String.format("<html><%s>%s</%s> | 拼团人数 <%s>%s</%s> | 备注:<%s>%s</%s></html>",
                PEER_TITLE_TIME_TAG,peerItem.time,PEER_TITLE_TIME_TAG,
                PeerTitleHtmlHandler.PEER_TITLE_NUM_TAG, peerItem.currentNum+"/"+peerItem.maxNum, PeerTitleHtmlHandler.PEER_TITLE_NUM_TAG,
                PEER_TITLE_REMARK_TAG,peerItem.remark,PEER_TITLE_REMARK_TAG);
        titleView.setText(Html.fromHtml(content, null, new PeerTitleHtmlHandler(titleView.getContext(),peerItem.currentNum>=peerItem.maxNum)));
        titleView.setMovementMethod(LinkMovementMethod.getInstance());//响应点击事件
    }

}
